package org.liaohailong.library.widget.refresh

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.TextView
import org.liaohailong.library.R

/**
 * Author: liaohailong
 * Date: 2019/4/25
 * Time: 22:03
 * Description: 下拉头部回调
 **/
class DefaultRefreshHeaderView : FrameLayout, IRefreshHeaderView {
    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(context, attributeSet, defStyle)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    private var arrowView: TextView
    private var infoView: TextView

    private var pull: Boolean = true

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_default_refresh_header_view, this, true)
        arrowView = findViewById(R.id.arrow_img)
        infoView = findViewById(R.id.info_txt)
    }

    override fun onPullBegin() {
        infoView.text = "下拉刷新"
    }

    override fun onPullProgress(progress: Float) {
        if (progress >= 1.0f) {
            if (pull) {
                val downAnim = ObjectAnimator.ofFloat(arrowView, "rotationX", 180f)
                downAnim.duration = 200
                downAnim.start()
                infoView.text = "松手后刷新"
                pull = false
            }
        } else {
            if (!pull) {
                val downAnim = ObjectAnimator.ofFloat(arrowView, "rotationX", 0f)
                downAnim.duration = 200
                downAnim.start()
                infoView.text = "下拉刷新"
                pull = true
            }
        }
    }

    override fun onRefreshBegin() {
        infoView.text = "正在刷新"
    }

    override fun onRefreshEnd() {
        infoView.text = "刷新完毕"
    }
}